<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="火车车次" prop="trainNumber">
        <el-input
          v-model="queryParams.trainNumber"
          placeholder="请输入火车车次"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="火车名称" prop="trainName">
        <el-input
          v-model="queryParams.trainName"
          placeholder="请输入火车名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发站" prop="departureStation">
        <el-input
          v-model="queryParams.departureStation"
          placeholder="请输入出发站"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="到达站" prop="arrivalStation">
        <el-input
          v-model="queryParams.arrivalStation"
          placeholder="请输入到达站"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出发时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeDepartureTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="到达时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeArrivalTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['boot:train:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['boot:train:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['boot:train:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="trainList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="火车ID" align="center" prop="trainId" />
      <el-table-column label="火车车次" align="center" prop="trainNumber" :show-overflow-tooltip="true"/>
      <el-table-column label="火车名称" align="center" prop="trainName" :show-overflow-tooltip="true"/>
      <el-table-column label="出发站" align="center" prop="departureStation" :show-overflow-tooltip="true"/>
      <el-table-column label="到达站" align="center" prop="arrivalStation" :show-overflow-tooltip="true"/>
      <el-table-column label="出发时间" align="center" prop="departureTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.departureTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到达时间" align="center" prop="arrivalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="列车型号" align="center" prop="trainType" :show-overflow-tooltip="true"/>
      <el-table-column label="总车厢数" align="center" prop="totalCarriages" width="104" :show-overflow-tooltip="true"/>
      <el-table-column label="火车图片" align="center" prop="trainImage" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.trainImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" :show-overflow-tooltip="true"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" :show-overflow-tooltip="true"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" width="90px" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['boot:train:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['boot:train:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改火车信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="trainRef" :model="form" :rules="rules">
        <el-form-item label="火车车次" prop="trainNumber">
          <el-input v-model="form.trainNumber" placeholder="请输入火车车次" />
        </el-form-item>
        <el-form-item label="火车名称" prop="trainName">
          <el-input v-model="form.trainName" placeholder="请输入火车名称" />
        </el-form-item>
        <el-form-item label="出发站" prop="departureStation">
          <el-input v-model="form.departureStation" placeholder="请输入出发站" />
        </el-form-item>
        <el-form-item label="到达站" prop="arrivalStation">
          <el-input v-model="form.arrivalStation" placeholder="请输入到达站" />
        </el-form-item>
        <el-form-item label="出发时间" prop="departureTime">
          <el-date-picker clearable
            v-model="form.departureTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择出发时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到达时间" prop="arrivalTime">
          <el-date-picker clearable
            v-model="form.arrivalTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择到达时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="列车型号" prop="trainType">
          <el-input v-model="form.trainType" placeholder="请输入列车型号" />
        </el-form-item>
        <el-form-item label="总车厢数" prop="totalCarriages">
          <el-input v-model="form.totalCarriages" placeholder="请输入总车厢数" type="number" />
        </el-form-item>
        <el-form-item label="火车图片" prop="trainImage">
          <image-upload v-model="form.trainImage"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Train">
import { listTrain, getTrain, delTrain, addTrain, updateTrain } from "@/api/boot/train"

const { proxy } = getCurrentInstance()

const trainList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const daterangeDepartureTime = ref([])
const daterangeArrivalTime = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    trainNumber: null,
    trainName: null,
    departureStation: null,
    arrivalStation: null,
    departureTime: null,
    arrivalTime: null,
  },
  rules: {
    trainId: [
      { required: true, message: "火车ID不能为空", trigger: "blur" }
    ],
    trainNumber: [
      { required: true, message: "火车车次不能为空", trigger: "blur" }
    ],
    trainName: [
      { required: true, message: "火车名称不能为空", trigger: "blur" }
    ],
    departureStation: [
      { required: true, message: "出发站不能为空", trigger: "blur" }
    ],
    arrivalStation: [
      { required: true, message: "到达站不能为空", trigger: "blur" }
    ],
    departureTime: [
      { required: true, message: "出发时间不能为空", trigger: "blur" }
    ],
    arrivalTime: [
      { required: true, message: "到达时间不能为空", trigger: "blur" }
    ],
    trainType: [
      { required: true, message: "列车型号不能为空", trigger: "blur" }
    ],
    totalCarriages: [
      { required: true, message: "总车厢数不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询火车信息列表 */
function getList() {
  loading.value = true
  queryParams.value.params = {}
  if (null != daterangeDepartureTime && '' != daterangeDepartureTime) {
    queryParams.value.params["beginDepartureTime"] = daterangeDepartureTime.value[0]
    queryParams.value.params["endDepartureTime"] = daterangeDepartureTime.value[1]
  }
  if (null != daterangeArrivalTime && '' != daterangeArrivalTime) {
    queryParams.value.params["beginArrivalTime"] = daterangeArrivalTime.value[0]
    queryParams.value.params["endArrivalTime"] = daterangeArrivalTime.value[1]
  }
  listTrain(queryParams.value).then(response => {
    trainList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    trainId: null,
    trainNumber: null,
    trainName: null,
    departureStation: null,
    arrivalStation: null,
    departureTime: null,
    arrivalTime: null,
    trainType: null,
    totalCarriages: null,
    trainImage: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("trainRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeDepartureTime.value = []
  daterangeArrivalTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.trainId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加火车信息"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _trainId = row.trainId || ids.value
  getTrain(_trainId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改火车信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["trainRef"].validate(valid => {
    if (valid) {
      if (form.value.trainId != null) {
        updateTrain(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTrain(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _trainIds = row.trainId || ids.value
  proxy.$modal.confirm('是否确认删除火车信息编号为"' + _trainIds + '"的数据项？').then(function() {
    return delTrain(_trainIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}



getList()
</script>
